var pageNo = 1;
var pageSize = 10;
const app = getApp();
var url = app.data.url;
var util = require('../../utils/util.js');
import Dialog from '@vant/weapp/dialog/dialog';
var map = new Map();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    currentTab: 0,
    merchantName: '',
    cart: [],
    merchantId: '',
    itemTypeList: [],
    itemTypeClickArr: [],
    classifyType: 0,
    queryType: 0,
    specShow: false
  },
  /**打开规格弹框 */
  openSpecPop: function (option) {
    let _that = this;
    let item = option.currentTarget.dataset.item;
    let order = option.currentTarget.dataset.order;
    item.order = order;
    // 加载
    _that.setData({
      specList: item.specList,
      specShow: true,
      submitStatus: true,
      itemName: item.title,
      itemPrice: item.payType == 1 ? item.discountPrice : item.price,
      tempItemPrice: item.payType == 1 ? item.discountPrice : item.price,
      item: item,
      result: []
    })
  },
  /**关闭规格弹框 */
  closeSpecPo: function () {
    let _that = this;
    _that.setData({
      specShow: false,
      submitStatus: false
    })
  },
  switchNav: function (e) {
    var page = this;
    var id = e.target.id;
    if (this.data.currentTab == id) {
      return false;
    } else {
      page.setData({
        currentTab: id
      });
    }
    page.setData({
      active: id
    });

  },
  
  onLoad: function(option) {
    var that = this;
    map = new Map();
    var merchantId = option.merchantId;
    // 查询菜品分类
    wx.request({
      url: url + '/ItemApi/queryItemListByMerchantId',
      data: {
        merchantId: merchantId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          var itemTypeOrder = res.data.data[0].classifyType;
          
          that.setData({
            itemTypeList: res.data.data,
            classifyType: itemTypeOrder
          })
          wx.setNavigationBarTitle({
            title: "我的商铺"
          })

          // 加载默认选中的菜单
          wx.request({
            url: url + '/ItemApi/queryItemByMerchantIdAndItemTypeId',
            data: {
              pageNo: pageNo,
              pageSize: pageSize,
              merchantId: merchantId,
              classifyType: itemTypeOrder
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
              console.log(res);
              if (res.data.code == 200) {
                that.setData({
                  cart: res.data.data
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: "fail",
                  duration: 2000
                })
              }

            },
            fail: function(res) {
              console.info('submit fail');
            },
            complete: function(res) {
              console.info('submit complete');
            }
          })
        } else {
          wx.showToast({
            title: '当前店铺无菜单',
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail1');
      },
      complete: function(res) {
        console.info('submit complete1');
      }
    })

    //查询商家餐桌信息
    wx.request({
      url: url + '/MerchantApi/queryMerchantDeskById',
      data: {
        id: merchantId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          if (res.data.data.length > 0) {
            that.setData({
              desk: res.data.data
            })
          }
        } else {
          wx.showToast({
            title: '加载餐桌失败',
            icon: "loading",
            duration: 2000
          })
        }

      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })
  },
  /**
   * 上拉触底加载更多数据
   */
  onReachBottom: function() {
    
  },

  /**
   * 监听用户下拉动作(下拉刷新)
   */
  onPullDownRefresh: function() {
  },
  
})